﻿'   ImageMeta.vb
'   WireBear Awesemulator
'
'   Created by Chris Kent
'   Copyright 2011 WireBear. All rights reserved
'
#Region "License Information"
'   WireBear Awesemulator is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   WireBear Awesemulator is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with WireBear Awesemulator - License.txt
'   If not, see <http://www.gnu.org/licenses/>.
#End Region


Imports System.Drawing.Imaging
Imports System.IO

Module ImageMeta

    'More codes can be found at http://www.exif.org/Exif2-2.PDF
    Public Enum PropertyIDs As Integer
        Software = 305
        Description = 270
        Artist = 315
        Copyright = 33432
    End Enum

    ''' <summary>
    ''' Set's an EXIF metadata property (string value) on an image
    ''' </summary>
    ''' <param name="img">The image to add the property too</param>
    ''' <param name="PropertyID">The property to set</param>
    ''' <param name="Value">The value of the property</param>
    Public Sub SetProperty(ByRef img As Bitmap, ByVal PropertyID As Integer, ByVal Value As String)
        img.SetPropertyItem(Image_CreateNewPropertyItem(PropertyID, Value))
    End Sub


#Region "Code taken from http://wrb.home.xs4all.nl/Articles/Article_IMG_Metadata_01.htm#070"

    ''' <summary>
    ''' Can't create a PropertyItem directly, so a constant image (with metadata) is rehydrated,
    ''' and a property item is reused and customized
    ''' </summary>
    ''' <param name="iId">The Property to set</param>
    ''' <param name="sValue">The value of the property</param>
    Public Function Image_CreateNewPropertyItem(ByVal iId As Integer, ByVal sValue As String) As PropertyItem
        Dim textConverter As New System.Text.ASCIIEncoding()
        Using oBitmap As Bitmap = Base64_To_Bitmap(Icon_Base_8_8)
            Dim propItems As PropertyItem() = oBitmap.PropertyItems
            If propItems.Length <> 0 Then
                propItems(0).Id = iId ' 0x010E = PropertyTagImageDescription.
                propItems(0).Type = CShort(2) ' Null-terminated ASCII string.
                propItems(0).Value = textConverter.GetBytes(sValue)
                propItems(0).Len = sValue.Length + 1
                Return propItems(0)
            Else
                Return Nothing
            End If
        End Using
    End Function

    ''' <summary>
    ''' Converts a byte string back into a bitmap
    ''' </summary>
    ''' <param name="sBase64">Image as a byte string</param>
    Private Function Base64_To_Bitmap(ByVal sBase64 As String) As Bitmap
        Dim bytData As [Byte]() = Convert.FromBase64String(sBase64)
        Using ms As New MemoryStream(bytData)
            Dim oB As New Bitmap(ms)
            ms.Close()
            Return oB
        End Using
    End Function

    ' Icon Base_8_8.png (8x8).
    Private Const Icon_Base_8_8 As String = "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAAARnQU1BAACx" & _
                                            "jwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABZJREFU" & _
                                            "KFNj/A8EDPgASAE+wDAsFAAAxQLjHkhOEUMAAAAASUVORK5CYIIAAAAAAAAAAAAAAAAAAAAAAAAA" & _
                                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" & _
                                            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="

#End Region

End Module
